#!/bin/bash
#
# Script for add, delete and manage users on remote servers
# Ermakov Konstantin 05.04.2011
#
# $Id: user_add.sh 150 2011-04-06 17:00:59Z mamartynov@gmail.com $
#

. $OPENARS_HOME/common/debug.h || exit 1
#. $OPENARS_HOME/common/functions.h || exit 1

user=$USER_ARS
[ -z "$user" ] && user=$LOGNAME
[ -z "$1" ] || [ -z "$2" ] && { ERR "Not enough parameters"; exit 1; }
[ ! -f "$1" ] && { ERR "not find file \"$1\""; exit 1; }
[ ! -f "$2" ] && { ERR "not find file \"$2\""; exit 1; }

srvlist=`cat $1`
uconffile=$2
INF "count hosts: `echo $srvlist | wc -w`"
adduser=$(awk -F= '$1=="logname" {print $2}' $uconffile)
INF "add user: $adduser"

function create_usetup() {
cat <<'CREATE_EOF'
#!/bin/bash

export PATH=$HOME/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
uconffile=$1

[ -z "$uconffile" ] && { echo "not enough parameter. exit 1"; exit 1; }
[ ! -f $uconffile ] && { echo "not found file $uconffile. exit 1"; exit 1; }

#basic param
logname=$(awk -F= '$1=="logname" {print $2}' $uconffile)
    [ -z "$logname" ] && { echo "not found parameter \"logname\". exit 1"; exit 1; }
pass=$(awk -F= '$1=="pass" {print $2}' $uconffile)
crypt_pass=$(awk -F= '$1=="crypt_pass" {print $2}' $uconffile)
    [ -z "$pass" -a -z "crypt_pass" ] && { echo "not found parameter \"pass/crypt_pass\". exit 1"; exit 1; }
#other param
uparam="-m"
homedir=$(awk -F= '$1=="homedir" {print $2}' $uconffile)
    [ ! -z "$homedir" ] && uparam="$uparam -d $homedir"
shell=$(awk -F= '$1=="shell" {print $2}' $uconffile)
    [ ! -z "$shell" ] && uparam="$uparam -s $shell"
group=$(awk -F= '$1=="group" {print $2}' $uconffile)
    [ ! -z "$group" ] && uparam="$uparam -g $group"
other_groups=$(awk -F= '$1=="other_groups" {print $2}' $uconffile)
    [ ! -z "$other_groups" ] && uparam="$uparam -G $other_groups"

if [ ! -z "$pass" ]; then
    sudo useradd $uparam $logname && echo -e "$pass\n$pass" | sudo passwd $logname 2>&1 
    ex_code=$?
else # [ ! -z "$crypt_pass" ]
    sudo useradd $uparam -p $crypt_pass $logname
    ex_code=$?
fi
getent passwd $logname

exit $ex_code

CREATE_EOF
}

usetup_dir=usetup
mkdir $TEMPDIR/$usetup_dir
cp $uconffile $TEMPDIR/$usetup_dir || exit 1

cd $TEMPDIR/$usetup_dir
create_usetup >> usetup.sh
[ $? -ne 0 ] && exit 1
chmod +x usetup.sh
cd $TEMPDIR
tar -cf $usetup_dir.tar $usetup_dir

ssh_command="tar -m -xf $usetup_dir.tar; cd $usetup_dir; ./usetup.sh `basename $uconffile`; ex_code=\$?; cd ..; rm -r $usetup_dir $usetup_dir.tar; exit \$ex_code"

for host in $srvlist
do
    (
    sftp -o BatchMode=yes $user@$host &> /dev/null << END
put $usetup_dir.tar
END
    sftp_code=$?
    [ $sftp_code -ne 0 ] && { WARN "Error sftp-connect or sftp-commands on $host: exit_code=$sftp_code"; exit 1; }
    SSH $user@$host "$ssh_command" out_info || ERR "Error useradd $adduser on server $host"
        echo "$out_info" > $TEMPDIR/out_info.out
    while read line; do
        [ ! -z "$line" ] && INF "$host: $line"
    done < $TEMPDIR/out_info.out
    ) &
done

WAIT 180

exit 0

